//
//  CropImageView.m
//  cropImage
//
//  Created by Nong Nghia on 7/1/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CropImageView.h"
#import <QuartzCore/QuartzCore.h>
#define MIN_SIZE 40
#define TOP_TAG 1
#define RIGHT_TAG 2
#define BOTTOM_TAG 3
#define LEFT_TAG 4

@implementation CropImageView
@synthesize zoomRatio;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        
        
        self.backgroundColor=[UIColor clearColor];
        topFrame=[[UIView alloc] initWithFrame:CGRectMake(
                                                          MIN_SIZE, 
                                                          0, 
                                                          self.frame.size.width-MIN_SIZE, 
                                                          MIN_SIZE)];
        [topFrame setBackgroundColor:[UIColor blackColor]];
        [topFrame setAlpha:0.4];
        [self addSubview:topFrame];

        rightFrame=[[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width-MIN_SIZE,
                                                            MIN_SIZE, 
                                                            MIN_SIZE, 
                                                            self.frame.size.height-MIN_SIZE)];
        [rightFrame setBackgroundColor:[UIColor blackColor]];
        [rightFrame setAlpha:0.4];
        [self addSubview:rightFrame];

        bottomFrame=[[UIView alloc] initWithFrame:CGRectMake(0,
                                                          self.frame.size.height-MIN_SIZE, 
                                                          self.frame.size.width-MIN_SIZE, 
                                                          MIN_SIZE)];
        [bottomFrame setBackgroundColor:[UIColor blackColor]];
        [bottomFrame setAlpha:0.4];
        [self addSubview:bottomFrame];

        leftFrame=[[UIView alloc] initWithFrame:CGRectMake(0, 
                                                          0, 
                                                          MIN_SIZE, 
                                                          self.frame.size.height-MIN_SIZE)];
        [leftFrame setBackgroundColor:[UIColor blackColor]];
        [leftFrame setAlpha:0.4];
        [self addSubview:leftFrame];
        
        topBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [topBtn setFrame:CGRectMake(0, 0, 40, 40)];
        [topBtn setImage:[UIImage imageNamed:@"camera_crop_height.png"] 
                forState:UIControlStateNormal];
        [topBtn setCenter:CGPointMake((
                                       rightFrame.frame.origin.x+leftFrame.frame.size.width)/2, 
                                      topFrame.frame.size.height
                                      )];
        topBtn.tag=TOP_TAG;

        rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setFrame:CGRectMake(0, 0, 40, 40)];
        [rightBtn setImage:[UIImage imageNamed:@"camera_crop_width.png"] 
                forState:UIControlStateNormal];
        [rightBtn setCenter:CGPointMake(
                                       rightFrame.frame.origin.x, 
                                      (topFrame.frame.size.height + bottomFrame.frame.origin.y)/2
                                      )];
        rightBtn.tag=RIGHT_TAG;

        bottomBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [bottomBtn setFrame:CGRectMake(0, 0, 40, 40)];
        [bottomBtn setImage:[UIImage imageNamed:@"camera_crop_height.png"] 
                forState:UIControlStateNormal];
        [bottomBtn setCenter:CGPointMake((
                                       rightFrame.frame.origin.x+leftFrame.frame.size.width)/2, 
                                      bottomFrame.frame.origin.y
                                      )];
        bottomBtn.tag=BOTTOM_TAG;

        leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [leftBtn setFrame:CGRectMake(0, 0, 40, 40)];
        [leftBtn setImage:[UIImage imageNamed:@"camera_crop_width.png"] 
                forState:UIControlStateNormal];
        [leftBtn setCenter:CGPointMake(
                                       leftFrame.frame.size.width, 
                                      (topFrame.frame.size.height + bottomFrame.frame.origin.y)/2
                                      )];
        leftBtn.tag=LEFT_TAG;
        UIPanGestureRecognizer *panTopGes=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(imagePaned:)];
        UIPanGestureRecognizer *panBottomGes=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(imagePaned:)];

        UIPanGestureRecognizer *panRightGes=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(imagePaned:)];

        UIPanGestureRecognizer *panLeftGes=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(imagePaned:)];

        [leftBtn addGestureRecognizer:panLeftGes];
        [rightBtn addGestureRecognizer:panRightGes];
        [topBtn addGestureRecognizer:panTopGes];
        [bottomBtn addGestureRecognizer:panBottomGes];
        
        [self addSubview:topBtn];
        [self addSubview:rightBtn];
        [self addSubview:bottomBtn];
        [self addSubview:leftBtn];
        
        centerFrame=[[UIView alloc] initWithFrame:CGRectMake(leftBtn.center.x, 
                                                             topBtn.center.y, 
                                                             (rightBtn.center.x-leftBtn.center.x), 
                                                             (bottomBtn.center.y-topBtn.center.y))];
        [centerFrame setBackgroundColor:[UIColor clearColor]];
        centerFrame.layer.borderWidth=2;
        centerFrame.layer.borderColor=[UIColor whiteColor].CGColor;
        [self insertSubview:centerFrame atIndex:0];
        

    }
    return self;
}


- (IBAction)imagePaned:(UIGestureRecognizer *)sender {
    UIPanGestureRecognizer *pan=(UIPanGestureRecognizer*)sender;
    CGPoint newPoint=[pan locationInView:self];
    if (newPoint.x <0 || newPoint.x>self.frame.size.width
        ||newPoint.y <0 || newPoint.y>self.frame.size.height) {
        return;
    }
    
    if (bottomBtn.center.y< topBtn.center.y+MIN_SIZE 
        && (pan.view.tag==BOTTOM_TAG || pan.view.tag==TOP_TAG)){
        if (pan.view.tag==BOTTOM_TAG) {
            bottomBtn.center=CGPointMake(bottomBtn.center.x, 
                                         bottomBtn.center.y+1);
        }
        else{
            topBtn.center=CGPointMake(topBtn.center.x, 
                                      topBtn.center.y-1);
        }        
        [self resetFrame];
        return;        
    }
    
    if (rightBtn.center.x < leftBtn.center.x+MIN_SIZE 
        && (pan.view.tag==RIGHT_TAG || pan.view.tag==LEFT_TAG)) {
        if (pan.view.tag==RIGHT_TAG) {
            rightBtn.center=CGPointMake(rightBtn.center.x+1, 
                                        rightBtn.center.y);

        }
        else {
            leftBtn.center=CGPointMake(leftBtn.center.x-1, 
                                       leftBtn.center.y);

        }
        [self resetFrame];
        return;
        
    }
//    DBLOG(@"[%d]:%f|%f",pan.view.tag,newPoint.x,newPoint.y);
    if ([[NSString stringWithFormat:@"%f",newPoint.x] isEqualToString:@"nan"]
        ||[[NSString stringWithFormat:@"%f",newPoint.y] isEqualToString:@"nan"]) {
        
        return;
    }
    if (pan.view.tag%2==0) {
        [pan.view setCenter:CGPointMake(newPoint.x, pan.view.center.y)];
    }
    else {
        [pan.view setCenter:CGPointMake(pan.view.center.x, newPoint.y)];
    }
    [self resetFrame];
}


-(void)resetFrame{
    
    [topFrame setFrame:CGRectMake(leftBtn.center.x,
                                  0,
                                  self.frame.size.width-leftBtn.center.x,
                                  topBtn.center.y)];
    
    [rightFrame setFrame:CGRectMake(rightBtn.center.x,
                                    topBtn.center.y,
                                    self.frame.size.width-rightBtn.center.x,
                                    self.frame.size.height-topBtn.center.y)];
    
    [bottomFrame setFrame:CGRectMake(0,
                                     bottomBtn.center.y,
                                     rightBtn.center.x, 
                                     self.frame.size.height-bottomBtn.center.y)];
    
    [leftFrame setFrame:CGRectMake(0,
                                   0,
                                   leftBtn.center.x, 
                                   bottomBtn.center.y)];
    
    [topBtn setCenter:CGPointMake((
                                   rightFrame.frame.origin.x+leftFrame.frame.size.width)/2, 
                                  topFrame.frame.size.height
                                  )];
    
    [rightBtn setCenter:CGPointMake(
                                    rightFrame.frame.origin.x, 
                                    (topFrame.frame.size.height + bottomFrame.frame.origin.y)/2
                                    )];

    [leftBtn setCenter:CGPointMake(
                                   leftFrame.frame.size.width, 
                                   (topFrame.frame.size.height + bottomFrame.frame.origin.y)/2
                                   )];
    
    [bottomBtn setCenter:CGPointMake((
                                      rightFrame.frame.origin.x+leftFrame.frame.size.width)/2, 
                                     bottomFrame.frame.origin.y
                                     )];
    
    [centerFrame setFrame:CGRectMake(leftBtn.center.x, 
                                     topBtn.center.y, 
                                     (rightBtn.center.x-leftBtn.center.x), 
                                     (bottomBtn.center.y-topBtn.center.y))];
}

-(CGRect)cropSize{
    return CGRectMake(leftBtn.center.x *zoomRatio, 
                      topBtn.center.y *zoomRatio, 
                      (rightBtn.center.x-leftBtn.center.x)*zoomRatio, 
                      (bottomBtn.center.y-topBtn.center.y)*zoomRatio);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
